/*
 * @(#)ExtImgTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.taglib;

import java.util.Hashtable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

/**
 * <p>Class Name:    ExtImgTag.java    </p>
 * <p>Description:   This tag is the layout the dms detail according to the documentID in DOCUMENT_ID
 * @author           Phoebe Wong
 * @company          DCIVision Limited
 * @creation date    12/08/2003
 * @version          $Revision: 1.34.26.2 $
 */

public class ExtImgTag extends BodyTagSupport {

  public static final String REVISION = "$Revision: 1.34.26.2 $";
  
  private static final Log log = LogFactory.getLog(ExtImgTag.class);
  
  private String sFileName;
  private String sDocType = DmsDocument.DOCUMENT_TYPE;
  private String sTarDocType = DmsDocument.DOCUMENT_TYPE;
  private String sExtension = null;
  private Integer nWidth = null;
  private Integer nHeight = null;
  public static Hashtable hashExt = new Hashtable();

  static {
    //file extension images
    hashExt.put("AVI", "IconDMSFileExtAVI.gif");
    hashExt.put("AVISHORTCUT", "IconDMSFileExtAVIShortcut.gif");
    hashExt.put("BMP", "IconDMSFileExtBMP.gif");
    hashExt.put("BMPSHORTCUT", "IconDMSFileExtBMPShortcut.gif");
    hashExt.put("CAB", "IconDMSFileExtCAB.gif");
    hashExt.put("CABSHORTCUT", "IconDMSFileExtCABShortcut.gif");
    hashExt.put("CDR", "IconDMSFileExtCDR.gif");
    hashExt.put("CDRSHORTCUT", "IconDMSFileExtCDRShortcut.gif");
    hashExt.put("CDT", "IconDMSFileExtCDR.gif");
    hashExt.put("CDTSHORTCUT", "IconDMSFileExtCDRShortcut.gif");
    hashExt.put("DOC", "IconDMSFileExtDOC.gif");
    hashExt.put("DOCSHORTCUT", "IconDMSFileExtDOCShortcut.gif");
    hashExt.put("EXE", "IconDMSFileExtEXE.gif");
    hashExt.put("EXESHORTCUT", "IconDMSFileExtEXEShortcut.gif");
    hashExt.put("FH7", "IconDMSFileExtFH7.gif");
    hashExt.put("FH7SHORTCUT", "IconDMSFileExtFH7Shortcut.gif");
    hashExt.put("GIF", "IconDMSFileExtGIF.gif");
    hashExt.put("GIFSHORTCUT", "IconDMSFileExtGIFShortcut.gif");
    hashExt.put("HTM", "IconDMSFileExtHTM.gif");
    hashExt.put("HTMSHORTCUT", "IconDMSFileExtHTMShortcut.gif");
    hashExt.put("HTML", "IconDMSFileExtHTM.gif");
    hashExt.put("HTMLSHORTCUT", "IconDMSFileExtHTMShortcut.gif");
    hashExt.put("JPG", "IconDMSFileExtJPG.gif");
    hashExt.put("JPGSHORTCUT", "IconDMSFileExtJPGShortcut.gif");
    hashExt.put("JPEG", "IconDMSFileExtJPG.gif");
    hashExt.put("JPEGSHORTCUT", "IconDMSFileExtJPGShortcut.gif");
    hashExt.put("MDB", "IconDMSFileExtMDB.gif");
    hashExt.put("MDBSHORTCUT", "IconDMSFileExtMDBShortcut.gif");
    hashExt.put("MPG", "IconDMSFileExtMPG.gif");
    hashExt.put("MPGSHORTCUT", "IconDMSFileExtMPGShortcut.gif");
    hashExt.put("MPEG", "IconDMSFileExtMPG.gif");
    hashExt.put("MPEGSHORTCUT", "IconDMSFileExtMPGShortcut.gif");
    hashExt.put("MPP", "IconDMSFileExtMPP.gif");
    hashExt.put("MPPSHORTCUT", "IconDMSFileExtMPPShortcut.gif");
    hashExt.put("NSF", "IconDMSFileExtNSF.gif");
    hashExt.put("NSFSHORTCUT", "IconDMSFileExtNSFShortcut.gif");
    hashExt.put("PDF", "IconDMSFileExtPDF.gif");
    hashExt.put("PDFSHORTCUT", "IconDMSFileExtPDFShortcut.gif");
    hashExt.put("PNG", "IconDMSFileExtPNG.gif");
    hashExt.put("PNGSHORTCUT", "IconDMSFileExtPNGShortcut.gif");
    hashExt.put("POT", "IconDMSFileExtPPT.gif");
    hashExt.put("POTSHORTCUT", "IconDMSFileExtPPTShortcut.gif");
    hashExt.put("PPT", "IconDMSFileExtPPT.gif");
    hashExt.put("PPTSHORTCUT", "IconDMSFileExtPPTShortcut.gif");
    hashExt.put("PSD", "IconDMSFileExtPSD.gif");
    hashExt.put("PSDSHORTCUT", "IconDMSFileExtPSDShortcut.gif");
    hashExt.put("RAR", "IconDMSFileExtRAR.gif");
    hashExt.put("RARSHORTCUT", "IconDMSFileExtRARShortcut.gif");
    hashExt.put("RTF", "IconDMSFileExtRTF.gif");
    hashExt.put("RTFSHORTCUT", "IconDMSFileExtRTFShortcut.gif");
    hashExt.put("TIF", "IconDMSFileExtTIF.gif");
    hashExt.put("TIFSHORTCUT", "IconDMSFileExtTIFShortcut.gif");
    hashExt.put("TXT", "IconDMSFileExtTXT.gif");
    hashExt.put("TXTSHORTCUT", "IconDMSFileExtTXTShortcut.gif");
    hashExt.put("XLS", "IconDMSFileExtXLS.gif");
    hashExt.put("XLSSHORTCUT", "IconDMSFileExtXLSShortcut.gif");
    hashExt.put("ZIP", "IconDMSFileExtZIP.gif");
    hashExt.put("ZIPSHORTCUT", "IconDMSFileExtZIPShortcut.gif");
    hashExt.put("CMP", "IconDMSFileExtCOMPOUND.gif");
    hashExt.put("CMPSHORTCUT", "IconDMSFileExtCOMPOUNDShort.gif");
    hashExt.put("PAPER","IconDMSPaperDoc.gif");
    hashExt.put("EMAIL","IconTreeEmailDocOpen.gif");
    hashExt.put("FORM","IconTreeFormDocOpen.gif");
    hashExt.put("WORKFLOW","IconTreeFlowDocOpen.gif");
    hashExt.put("EML","IconDMSEmailDoc.gif");
    hashExt.put("EMLSHORTCUT","IconDMSEmailDocShortcut.gif");
  }

  /**
   * Constructor
   */
  public ExtImgTag() {
    super();
  }

  public String getFileName() {
    return this.sFileName;
  }

  public void setFileName(String sFileName) {
    this.sFileName = sFileName;
  }

  public String getDocType() {
    return this.sDocType;
  }

  public void setDocType(String sDocType) {
    this.sDocType = sDocType;
  }
  
  public String getTarDocType() {
    return this.sTarDocType;
  }

  public void setTarDocType(String sTarDocType) {
    this.sTarDocType = sTarDocType;
  }

  public String getExtension() {
    return this.sExtension;
  }

  public void setExtension(String sExtension) {
    this.sExtension = sExtension;
  }

  public Integer getHeight() {
    return this.nHeight;
  }

  public void setHeight(Integer nHeight) {
    this.nHeight = nHeight;
  }

  public Integer getWidth() {
    return this.nWidth;
  }

  public void setWidth(Integer nWidth) {
    this.nWidth = nWidth;
  }

  /**
   * Method called at start of tag
   * @return a SKIP_BODY
   */
  public int doStartTag() throws JspException {
    return SKIP_BODY;
  }

  public int doEndTag() throws JspException{
    JspWriter out = pageContext.getOut();
    String sIcon = null;
    String sExt = "0";
    if (Utility.isEmpty(this.sExtension)) {
      sExt = TextUtility.getExtension(sFileName);
    } else {
      sExt = this.sExtension;
    }

    if (sDocType.equals(DmsDocument.COMPOUND_DOC_TYPE)) {
      sExt = "CMP";
      this.setExtension("CMP");
    }
    if (sDocType.equals(DmsDocument.PAPER_DOC_TYPE)){
      sExt="PAPER";
      this.setExtension("PAPER");
    }
    if (sDocType.equals(DmsDocument.EMAIL_DOC_TYPE)){
      sExt="EML";
      this.setExtension("EML");
    }
    try {
      if (Utility.isEmpty(sExt)) {
        sIcon = null;
      } else {
        sIcon = (String)hashExt.get(sExt);
        if (Utility.isEmpty(sIcon)){
          sIcon = "IconDMSFileExtUNKNOWN.gif";  
        }
      }
      //appended the Shortcut name to get the shortcut image
      if (sDocType.equals(DmsDocument.DOCUMENT_LINK)) {
      	if (sTarDocType.equals(DmsDocument.FOLDER_TYPE)) {
          sIcon = "IconDMSFolderShortcut.gif";
      	}else{
      	  sIcon = (String)hashExt.get(sExt.concat("SHORTCUT"));
      	}
      }
      if (sDocType.equals(DmsDocument.FOLDER_TYPE)) {
        out.print("<img ");
        if (this.nHeight != null) {
          out.print("height=\"" + this.nHeight + "\" ");
        }
        if (this.nWidth != null) {
          out.print("width=\"" + this.nWidth + "\" ");
        }
        if (sDocType.equals(DmsDocument.DOCUMENT_LINK)) {
          out.print("src=\"" + imgSrc("IconDMSFolderShortcut.gif") + "\" ");
        }else{
          out.print("src=\"" + imgSrc("IconDMSFolder.gif") + "\" ");
        }
        out.print("align=\"absmiddle\" ");
        out.print("/>");
      } else {
        if (!"".equals(sIcon) && !Utility.isEmpty(sIcon)) {
          out.print("<img ");
          if (this.nHeight != null) {
            out.print("height=\"" + this.nHeight + "\" ");
          }
          if (this.nWidth != null) {
            out.print("width=\"" + this.nWidth + "\" ");
          }
          out.print("src=\""+imgSrc(sIcon)+"\" ");
          out.print("align=\"absmiddle\" ");
          out.print("/>");
        } else {
          out.print("<img ");
          if (this.nHeight != null) {
            out.print("height=\"" + this.nHeight + "\" ");
          }
          if (this.nWidth != null) {
            out.print("width=\"" + this.nWidth + "\" ");
          }
          if (sDocType.equals(DmsDocument.DOCUMENT_LINK)) {
            out.print("src=\"" + imgSrc("IconDMSFileExtUNKNOWNShortcut.gif") + "\" ");
          }else{
            out.print("src=\"" + imgSrc("IconDMSFileExtUNKNOWN.gif") + "\" ");
          }
          out.print("align=\"absmiddle\" ");
          out.print("/>");
        }
      }
    } catch (java.io.IOException ioe) {
      log.error(ioe, ioe);
      throw new JspException(ioe);
    }
    this.setExtension("");
    return EVAL_PAGE;
  }

  protected String imgSrc(String icon) throws javax.servlet.jsp.JspException {
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    if ((sessionContainer!=null) && (sessionContainer.getUserRecord()!=null)) {
      if (sessionContainer.getUserRecord().getPreference()!=null) {
        preference = sessionContainer.getUserRecord().getPreference().toString();
      }
    }
    return (contextPath + "/theme/pref" + preference + "/img/common/" + icon);
  }
  
  protected String imgSrc() throws javax.servlet.jsp.JspException {
    String icon = "";    
    String sExt = "0";
    
    if (Utility.isEmpty(this.sExtension)) {
      sExt = TextUtility.getExtension(sFileName);
    } else {
      sExt = this.sExtension;
    }
    
    if (Utility.isEmpty(sExt)) {
      icon = null;
    } else {
      icon = (String)hashExt.get(sExt);
      if (Utility.isEmpty(icon)){
        icon = "IconDMSFileExtUNKNOWN.gif";  
      }
    }    
    
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    if ((sessionContainer!=null) && (sessionContainer.getUserRecord()!=null)) {
      if (sessionContainer.getUserRecord().getPreference()!=null) {
        preference = sessionContainer.getUserRecord().getPreference().toString();
      }
    }
    return (contextPath + "/theme/pref" + preference + "/img/common/" + icon);
  }

  public void release() {
    this.sFileName = null;
    this.sDocType = DmsDocument.DOCUMENT_TYPE;
    this.sExtension = null;
    this.nWidth = null;
    this.nHeight = null;
    super.release();
  }
}
